#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
bool printA = true;

// Function to print a letter, either 'A' or 'B'
void printLetter(char letter, bool isA) {
    // Loop 10 times
    for (int i = 0; i < 10; ++i) {
        // Lock the mutex to ensure thread safety
        std::unique_lock<std::mutex> lock(mtx);
        // Wait until the condition variable is notified and the condition is true
        cv.wait(lock, [isA]() { 
            // The condition: if it's 'A's turn and isA is true, or if it's 'B's turn and isA is false
            return (isA && printA) || (!isA && !printA); 
        });
        // Print the letter
        std::cout << letter << std::endl;
        // Switch the turn to the other letter
        printA = !isA;
        // Unlock the mutex
        lock.unlock();
        // Notify all waiting threads
        cv.notify_all();
    }
}

int main() {
    std::thread threadA(printLetter, 'A', true);
    std::thread threadB(printLetter, 'B', false);

    threadA.join();
    threadB.join();

    return 0;
}
